package com.ptit.piclient;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Toast;

import com.ptit.piclient.model.Settings;
import com.ptit.piclient.sp.SPUtils;
import com.ptit.piclient.tools.PiConnector;

public class SettingsActivity extends Activity implements OnClickListener, OnSeekBarChangeListener, OnCheckedChangeListener{
	EditText txtPiURL, txtPiPort;
	CheckBox chkShuffled, chkMuteSound;
	RadioGroup rgRepeat;
	RadioButton rdRepeatOne,rdRepeatAll,rdRepeatNone;
	Button btnSave;
	SeekBar seekBarVolume;
	
	boolean chechIfURLandPortAvailable = false;
	@Override
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_settings);		
		btnSave = (Button) findViewById(R.id.btnSave);
		txtPiURL  = (EditText) findViewById(R.id.txtPiURL);
		txtPiPort  = (EditText) findViewById(R.id.txtPiPort);
		chkShuffled = (CheckBox) findViewById(R.id.chkShuffled);
		chkMuteSound = (CheckBox) findViewById(R.id.chkMuteSound);
		
		rdRepeatAll = (RadioButton) findViewById(R.id.rdRepeatAll);
		rdRepeatNone = (RadioButton) findViewById(R.id.rdRepeatNone);
		rdRepeatOne = (RadioButton) findViewById(R.id.rdRepeatOne);
		
		rgRepeat = (RadioGroup) findViewById(R.id.rgRepeat);		
		seekBarVolume = (SeekBar) findViewById(R.id.seekBarVolume);
		seekBarVolume.setOnSeekBarChangeListener(this);
		btnSave.setOnClickListener(this);
		chkMuteSound.setOnCheckedChangeListener(this);
		// khuongdv oct 4 start
		String piURL = SPUtils.getPiUrlFromSP(getApplicationContext());
		int piPort = SPUtils.getPiPortFromSP(getApplicationContext());
		if(piURL.equals("") == false) txtPiURL.setText(piURL);
		if(piPort >-1) txtPiPort.setText(piPort+"");
		//khuongdv oct4 end
	}

	@Override
	protected void onResume(){
		super.onResume();
//		new Timer().schedule(new RequestSettingsInfor(this), 0,1500);
	}
	
	@Override
	public void onClick(View v) {
		if(v.equals(btnSave)){
			if(txtPiURL.getText() == null 
					|| txtPiURL.getText().toString().equals("") 
					|| txtPiPort.getText() == null 
					|| txtPiPort.getText().toString().equals("")) {
				Toast.makeText(this, "Please Enter Pi URL and port", Toast.LENGTH_LONG).show();
				return;
			}
			new CheckURLAndPort(this).execute();
		}
	}
	private class CheckURLAndPort extends AsyncTask<Void, Void, Boolean>{
		Activity act;
		public CheckURLAndPort(Activity a){
			act = a;
			Toast.makeText(act, "Start checking...", 0).show();
		}
		@Override
		protected Boolean doInBackground(Void... params) {
			Boolean check = PiConnector.getInstance(act).checkURLandPort(
					txtPiURL.getText().toString(),
					Integer.parseInt(txtPiPort.getText().toString()));
			return check;
		}
		@Override
		protected void onPostExecute (Boolean result){
			chechIfURLandPortAvailable = result;
			if(chechIfURLandPortAvailable == true){
				Settings settings = new Settings();			
				settings.setPiPort(Integer.parseInt(txtPiPort.getText().toString()));
				settings.setPiUrl(txtPiURL.getText().toString());			
				// khuongdv Oct 3
				Log.i("START INSERTING", "YES");
				SPUtils.insertSettingsToSP(getApplicationContext(), settings);
				Log.i("END INSERTING","YES");
				PiConnector.getInstance(act).setPiURL("http://" + settings.getPiUrl() + ":" + settings.getPiPort());
				startActivity(new Intent(act,MainActivity.class));
				//khuongdv Oct 3
				Toast.makeText(act, "Ok", 1).show();
			}
			else {
				Toast.makeText(act, "@@@@", 1).show();
			}
		}
	}
	@Override
	public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
		int volume = seekBarVolume.getProgress();
		new ChangeVolume(this,volume).execute();
	}
	class ChangeVolume extends AsyncTask<Void, Void, Void>{
		Activity act;
		int volume;
		public ChangeVolume(Activity ac, int vol){
			act = ac;
			volume = vol;
		}
		@Override
		protected Void doInBackground(Void... params) {
			PiConnector.getInstance(act).setVolume(volume);
			return null;
		}
		
	}
	@Override
	public void onStartTrackingTouch(SeekBar arg0) {
		
	}

	@Override
	public void onStopTrackingTouch(SeekBar arg0) {
		
	}
//	/**
//	 * TimerTask that frequently send request to server to retrieve settings info
//	 */
//	private class RequestSettingsInfor extends TimerTask{
//		private Activity act;
//		public RequestSettingsInfor(Activity a){
//			act = a;
//		}
//		@Override
//		public void run() {
//			new GetSettingsInfor(act).execute();
//		}		
//	}
//	//an asynctask will be executed in run() method of RequestSettingsInfor
//		private class GetSettingsInfor extends AsyncTask<Void, Void, Settings>{
//			
//			Activity act;
//			public GetSettingsInfor(Activity a){act = a;}
//			@Override
//			protected Settings doInBackground(Void... params) {
//				Settings settings = new Settings();
//				settings = PiConnector.getInstance().getCurrentSettings();
////				Log.e("settings-muted",settings.isMuted() + "" + settings.getRepeat() + "");
//				return settings;
//			}
//			@Override
//			protected void onPostExecute(Settings settings){
//				if(settings == null) return;
//				chkMuteSound.setChecked(settings.isMuted());
//				chkShuffled.setChecked(settings.isShuffled());				
//				if(settings.getRepeat() == Settings.REPEAT_ALL) rdRepeatAll.setChecked(true);		
//				if(settings.getRepeat() == Settings.REPEAT_ONE) rdRepeatOne.setChecked(true);
//				if(settings.getRepeat() == Settings.REPEAT_NONE) rdRepeatNone.setChecked(true);
//				seekBarVolume.setProgress(settings.getVolume());		
//			}
//		}
		@Override
		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			if(buttonView.equals(chkMuteSound)){
				
			}
		}
}